package com.sto.stoapp.thread;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.sto.stoapp.db.StoFilePreference;
import com.sto.stoapp.http.BaseMessage;
import com.sto.stoapp.http.HttpManager;
import com.sto.stoapp.http.URLActions;
import com.sto.stoapp.http.request.LoginRequest;
import com.sto.stoapp.http.response.LoginResponse;
import com.sto.stoapp.util.TraceUtil;

import android.content.Context;

/**
 * 下载前的登录线程
 * 
 * @author Administrator
 * 
 */
public class SimpleLoginThread extends BaseThread {
	Context context;

	public SimpleLoginThread(Context context) {
		super(context);
		this.context = context;
	}

	@Override
	public void run() {
		super.run();

		LoginRequest request = new LoginRequest(context, StoFilePreference
				.getInstance(context).getUserCode(), StoFilePreference
				.getInstance(context).getPwdInfo(), TraceUtil.getTraceTimes());
		HttpManager mgr = new HttpManager();
		Gson gs = new Gson();
		String str = mgr.doPostClient(request.buildToRequestString(),
				StoFilePreference.getInstance(context).getServerIp()
						+ URLActions.LOGIN_ACTION);
		LoginResponse response = null;
		try {
			response = gs.fromJson(str, new TypeToken<LoginResponse>() {
			}.getType());
		} catch (JsonSyntaxException e) {
			e.printStackTrace();
		}
		if (response != null
				&& response.respCode.equals(BaseMessage.REQUEST_SUCCESS)) {
			// setTime(response.systemTime);
			StoFilePreference.getInstance(context).setSessionId(
					response.sessionId);
		}
	}
}
